﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace UCF.Core.Data
{
    /// <summary>
    /// 2013-6-6 by sutroon
    /// </summary>
    public class DataModule
    {
        public Dictionary<string, string> Params { get; set; }
        public List<string> MessageSet { get; set; }
        private string tableName;
        private DataTable dtSet;
        public DataModule(string tblname)
        {
            this.Params = new Dictionary<string, string>();
            MessageSet = new List<string>();
            this.tableName = tblname;
        }

        public string GetParam(string name)
        {
            if (!this.Params.ContainsKey(name)) return string.Empty;
            Type t = this.Params[name].GetType();
            if (t == typeof(string))
            {
                return this.Params[name].ToString().Trim('\'');
            }
            return this.Params[name];
        }

        public string GetString(string name)
        {
            return dtSet.Rows[0][name] == DBNull.Value ? string.Empty : dtSet.Rows[0][name].ToString();
        }

        public void AddParam(string name, object value, string errmsg = "", string type = "string")
        {
            if (value == null)
            {
                if (errmsg.Length > 0) MessageSet.Add(errmsg);
                return;
            }
            string v = value.ToString();
            bool isStr = false;
            switch (type.ToLower())
            {
                case "int":
                    int iV;
                    if (!Int32.TryParse(v, out iV))
                    {
                        if (errmsg.Length > 0) MessageSet.Add(errmsg);
                        return;
                    }
                    break;
                case "float":
                    float fV;
                    if (!float.TryParse(v, out fV))
                    {
                        if (errmsg.Length > 0) MessageSet.Add(errmsg);
                        return;
                    }
                    break;
                case "datetime":
                    DateTime dtV;
                    if (!DateTime.TryParse(v, out dtV))
                    {
                        if (errmsg.Length > 0) MessageSet.Add(errmsg);
                        return;
                    }
                    isStr = true;
                    break;
                default:
                    if (v == null || v.IndexOf("'") > -1)
                    {
                        if (errmsg.Length > 0) MessageSet.Add(errmsg);
                        return;
                    }
                    isStr = true;
                    break;
            }
            this.Params[name] = isStr ? string.Format("'{0}'", v) : v;
        }
        public string GetError(string splitor = "")
        {
            if (MessageSet.Count == 0) return string.Empty;
            if (splitor.Length == 0) splitor = Environment.NewLine;
            return MessageSet.ToString(splitor);
        }

        public string GetInsertSql()
        {
            StringBuilder sbFields = new StringBuilder(), sbValue = new StringBuilder();
            foreach (string k in Params.Keys)
            {
                sbFields.AppendFormat(" {0},", k);
                sbValue.AppendFormat(" {0},", Params[k]);
            }
            this.Params.Clear();
            return string.Format("Insert into {0} ({1}) values ({2});", this.tableName, sbFields.ToString().TrimEnd(','), sbValue.ToString().TrimEnd(','));
        }

        public string GetUpdateFieldsSql()
        {
            StringBuilder sb = new StringBuilder();
            foreach (string k in Params.Keys)
            {
                sb.AppendFormat("{0}={1},", k, Params[k]);
            }
            this.Params.Clear();
            return sb.ToString().Trim(',');
        }

        public void AddMessage(string msg)
        {
            MessageSet.Add(msg);
        }

        public void Clear()
        {
            this.Params.Clear();
            this.MessageSet.Clear();
        }
    }
}
